//
//  Factory.m
//
//  i love nancy.
//  Created by starfish on 15/9/2.
//  Copyright (c) 2015年 小R. All rights reserved.
//

#import "Factory.h"
#import "MyHelper.h"
#define MRC 0

@implementation Factory
//创建Button的工厂，将特殊的元素传入，生产相对应的Button
+ (UIButton *)createButtonWithTitle:(NSString *)title frame:(CGRect)frame target:(id)target selector:(SEL)selector {
    return [self createButtonWithTitle:title frame:frame backgroundColor:RGB(0.3, 0.8, 1.f) target:target selector:selector];
}
+ (UIButton *)createButtonWithTitle:(NSString *)title frame:(CGRect)frame backgroundColor:(UIColor *)color target:(id)target selector:(SEL)selector {
    return [self createButtonWithTitle:title frame:frame backgroundColor:color fontSize:14.f target:target selector:selector];
}
+ (UIButton *)createButtonWithTitle:(NSString *)title frame:(CGRect)frame backgroundColor:(UIColor *)color fontSize:(CGFloat)size target:(id)target selector:(SEL)selector {
    return [self createButtonWithTitle:title frame:frame backgroundColor:color fontSize:size image:nil target:target selector:selector];
}
+ (UIButton *)createButtonWithTitle:(NSString *)title frame:(CGRect)frame backgroundColor:(UIColor *)color fontSize:(CGFloat)size image:(UIImage *)image target:(id)target selector:(SEL)selector {
    return [self createButtonWithTitle:title frame:frame backgroundColor:color fontSize:size image:image backgroundImage:nil target:target selector:selector];
}
+ (UIButton *)createButtonWithTitle:(NSString *)title frame:(CGRect)frame backgroundColor:(UIColor *)color fontSize:(CGFloat)size backgroundImage:(UIImage *)bgImage target:(id)target selector:(SEL)selector {
    return [self createButtonWithTitle:title frame:frame backgroundColor:color fontSize:size backgroundImage:bgImage target:target selector:selector];
}
+ (UIButton *)createButtonWithTitle:(NSString *)title
                              frame:(CGRect)frame
                    backgroundColor:(UIColor *)color
                           fontSize:(CGFloat)size
                              image:(UIImage *)image
                    backgroundImage:(UIImage *)backgroundImage
                             target:(id)target
                           selector:(SEL)selector {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    
    button.layer.cornerRadius = 3.f;
    button.layer.masksToBounds = YES;
    
    button.titleLabel.font = [UIFont systemFontOfSize:size];
    button.backgroundColor = color;
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:image forState:UIControlStateNormal];
    [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return button;
}

//创建Label的工厂，将特殊的元素传入，生产相对应的Label
+ (UILabel *)createLabelWithTitle:(NSString *)title frame:(CGRect)frame {
    return [self createLabelWithTitle:title frame:frame fontSize:14.f];
}
+ (UILabel *)createLabelWithTitle:(NSString *)title frame:(CGRect)frame textColor:(UIColor *)color {
    return [self createLabelWithTitle:title frame:frame textColor:color fontSize:14.f];
}
+ (UILabel *)createLabelWithTitle:(NSString *)title frame:(CGRect)frame fontSize:(CGFloat)size {
    return [self createLabelWithTitle:title frame:frame textColor:[UIColor blackColor] fontSize:size];
}
+ (UILabel *)createLabelWithTitle:(NSString *)title frame:(CGRect)frame textColor:(UIColor *)color fontSize:(CGFloat)size {
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = title;
    label.textColor = color;
    label.font = [UIFont systemFontOfSize:size];
#if MRC
    return [label autorelease];
#else
    return label;
#endif
}

//创建View的工厂，将特殊的元素传入，生产相应的View
+ (UIView *)createViewWithBackgroundColor:(UIColor *)color frame:(CGRect)frame {
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = color;
#if MRC
    return [view autorelease];
#else
    return view;
#endif
}

//创建textField的工厂，将特殊的元素传入，生产响应的textField
+ (UITextField *)createViewWithText:(NSString *)text frame:(CGRect)frame placeholder:(NSString *)placeholder textColor:(UIColor *)color borderStyle:(UITextBorderStyle)borderStyle {
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    textField.placeholder = placeholder;
    textField.borderStyle = borderStyle;
    textField.text = text;
    textField.textColor = color;
#if MRC
    return [textField autorelease];
#else
    return textField;
#endif
}

+ (UIImage *)createUIImageWithResource:(NSString *)resource andType:(NSString *)type{
    return [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:resource ofType:type]];
}

+ (UITextField *)createTextField:(CGRect)frame{
    UITextField * tempTextField = [[UITextField alloc]initWithFrame:frame];
    tempTextField.borderStyle = UITextBorderStyleRoundedRect;
    tempTextField.backgroundColor = RGB(170, 190, 241);
    tempTextField.textColor = RGB(16, 20, 193);
    tempTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    tempTextField.returnKeyType = UIReturnKeyNext;
    return tempTextField;
}

//9月29新增，view晃动
+ (void)addShakeAnimationForView:(UIView *)view{
    CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
    CGFloat currentTx = view.transform.tx;
    animation.delegate = self;
    animation.duration = .5f;
    animation.values = @[ @(currentTx), @(currentTx + 10), @(currentTx-8), @(currentTx + 8), @(currentTx -5), @(currentTx + 5), @(currentTx) ];
    animation.keyTimes = @[ @(0), @(0.225), @(0.425), @(0.6), @(0.75), @(0.875), @(1) ];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [view.layer addAnimation:animation forKey:NULL];
}

@end







